package com.sto.stoapp.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;

import sto.android.app.StoTongJNI;

/**
 * Created by wj on 2016-12-01.
 *
 * 卸载旧包的工具类
 */

public class UnInstallUtil {

    /**
     * 根据包名判断应用是否存在
     * @param pName
     * @return
     */
    public static boolean checkPackage(Context context ,String pName){

        if(pName==null || "".equals(pName)){
            return false;
        }

        try {
            context.getPackageManager().getApplicationInfo(pName,
                    PackageManager.GET_UNINSTALLED_PACKAGES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 卸载旧应用
     */
    public static void unInstallApp(Context context ,String pName){

        if(checkPackage(context,pName)){
            Uri uri=Uri.parse("package:" + pName);  //获取删除包名的URI
            Intent intent = new Intent(Intent.ACTION_DELETE, uri);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        }

    }

    /**
     * 获取应用的包名
     * @param context
     * @return
     */
    public static String getPackageName(Context context){

        String pdaBrand = StoTongJNI.getmPdaBrand();

        if (pdaBrand.equals("kaicom")) {

            return "com.kaicom.stoapp.activity";
        } else if (pdaBrand.equals("urovo")) {
            //"优博讯";
            return "";
        } else if (pdaBrand.equals("rongxin")) {
            //"荣信";
            return "com.cneop.sto";
        } else if (pdaBrand.equals("wangfeng")) {
            //"网风";
            return "com.cneop.stoExpress.cneops";
        } else {
            //"凯立";
            return "com.kaicom.stoapp.activity";
        }
    }
}
